{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Generative Adversarial Networks\n",
    "==================\n",
    "\n",
    "In this jupyter notebook we will see how to implement a **Generative Adversarial Network (GAN)** in Tensorflow. The references for this tutorial are the [paper](https://arxiv.org/pdf/1406.2661.pdf) of Goodfellow et al. (2014) and the [tutorial](https://arxiv.org/pdf/1701.00160.pdf) presented at NIPS. Here we will train a GAN on the CIFAR-10 dataset. The dataset can be downloaded and prepared following [this tutorial](../cifar10/cifar10.ipynb) on the TensorBag repository. The following are the software requirements necessary for running the code:\n",
    "\n",
    "- Python 2.7 [[install]](https://www.python.org/)\n",
    "- Tensorflow >= 1.5 [[install]](https://www.tensorflow.org/install/)\n",
    "- Numpy [[install]](https://scipy.org/install.html)\n",
    "\n",
    "To check your tensorflow version and all the software requirements run the following snippet:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensorflow version: 1.5.0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Couldn't import dot_parser, loading of dot files will not be possible.\n"
     ]
    }
   ],
   "source": [
    "import pickle\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "print(\"Tensorflow version: \" + str(tf.__version__))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Overview of the GAN architecture\n",
    "------------------------------------\n",
    "\n",
    "GANs are generative models based on the idea of a competitive [zero-sum game](https://en.wikipedia.org/wiki/Zero-sum_game) between two networks. The first network is called **generator** and its goal is to produce images that are as close as possible to real samples. The second network is called **discriminator** and is used to distinguish between the real images and the candidates produced by the generator. Here I report an intuitive example taken from the article of Goodfellow et al. (2014):\n",
    "\n",
    "*\"The generative model can be thought of as analogous to a team of counterfeiters, trying to produce fake currency and use it without detection, while the discriminative model is analogous to the police, trying to detect the counterfeit currency. Competition in this game drives both teams to improve their methods until the counterfeits are indistiguishable from the genuine articles.\"*\n",
    "\n",
    "\n",
    "<p align=\"center\">\n",
    "<img src=\"../etc/img/gan_problems.png\" width=\"700\">\n",
    "</p>\n",
    "\n",
    "**Problems**: altough the generative network worked surprisingly well in many conditions, some problems have been observed. (a) The first problems is counting. Some images of animals had multiple eyes and mouths. (b) The second problem is perspective. There were objects represented from multiple perspectives at the same time. (c) Finally there was a problem with the global structure. Distinctive parts of the object were represented with abnormal proportions and stitched together.\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Implementing the model in Tensorflow\n",
    "------------------------------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Training the model\n",
    "---------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Generating new samples\n",
    "-------------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Conclusions\n",
    "-------------\n",
    "\n",
    "**Copyright (c)** 2018 Massimiliano Patacchiola, MIT License"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
